/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;









/**
 * Customize
 */
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.model.User;
import vn.webapp.model.Users;
import vn.webapp.service.UserService;
import vn.webapp.validation.UserValidation;

@Controller("cpUser")
@RequestMapping(value = {"/cp"})
public class UserController extends BaseWeb {

    @Autowired
    private UserService userService;
    private Integer nav = 0;

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String listUsers(ModelMap model, HttpSession session) {
    	DataPage<Users> usersData = userService.list();
    	
    	List<Users> usersList = usersData.getData();
    	model.put("listUsers", usersList);
    	model.put("pageActive", "users");
        return "cp.users";
    }
    
    /**
    *
    * @param model
    * @return
    */
   @RequestMapping(value = "/add-an-user", method = RequestMethod.GET)
   public String addAnUser(ModelMap model, HttpSession session) {
	   
	   model.put("userFormAdd", new UserValidation());
   	   model.put("pageActive", "users");
       return "cp.addAnUser";
   }
   
   @RequestMapping(value="save-an-user", method=RequestMethod.POST)
   public String saveAnUser(@Valid @ModelAttribute("userFormAdd") UserValidation userValid, BindingResult result,  Map model, HttpSession session) {
	   model.put("pageActive", "users");
       if(result.hasErrors()) {
           return "cp.addAnUser";
       }else
       {
    	   String username = userValid.getUsername();
    	   String password = userValid.getPassword();
    	   String email = userValid.getEmail();
    	   String role = userValid.getRole();
    	   String salt = "salt-sequence";
    	   int activated = userValid.getActivated();
    	   
    	   User o_Username = userService.loadUserByUsername(username);
    	   if(o_Username != null)
           {
               model.put("err", "The username is exists.");
               return "cp.addAnUser";
           }
    	   int i_InsertUser = userService.saveAUser(username, password, salt, email, role, activated);
    	   
           model.put("status", "Successfully saved user: " + username);
           return "cp.addAnUser";
       }
   }
   
   @RequestMapping("/userdetail/{id}")
   public String editAnUser(ModelMap model, @PathVariable("id") int id) {
	   
	   List<Users> user = userService.viewDetail(id);
       model.put("userFormEdit", new UserValidation());
       model.put("dataUser", user.get(0));
	   model.put("pageActive", "users");
       return "cp.editAnUser";
   }
   
   
   /**
   *
   * @param validationForm
   * @param result
   * @param model
   * @return
   */
  @RequestMapping(value = "/edit-user-detail", method = RequestMethod.POST)
  public String processValidatinForm(@Valid @ModelAttribute("userFormEdit") UserValidation userFormEdit, BindingResult result, Map model, HttpSession session) {
      
      // Add the saved validationForm to the model  
      model.put("nav", this.nav);
      if (result.hasErrors()) {
         return "cp.editAnUser";
      }else
      {
          String username = userFormEdit.getUsername();
          String password = userFormEdit.getPassword();
          String role = userFormEdit.getRole();
          String email = userFormEdit.getEmail();
          int activated = userFormEdit.getActivated();
          int userId = userFormEdit.getUserId();
          
          Users o_Username = userService.loadUserByUsernameAndId(username, userId);
          if(o_Username != null)
          {
        	  model.put("err", "The username is exists.");
              return "cp.editAnUser";
          }
          userService.editAnUser(username, password, email, role, activated);
          model.put("status", "Successfully edited user: " + username);
          return "cp.editAnUser";
      }
  }

}
